/* This is not the full source, nor should you expect it to compile. For more information, consult the Lotus Notes SDK - ed/wgi */
//Globals
FILE *outfile = NULL;
long uniqueLabelCounter = 0;
char *field_text;
//Main
/* This standalone program is declared with argc and argv, even though these arguments cannot be used in the MacOS, unless it were to be built as an MPW tool. Aside from the conditional code for initializing the MacOS toolbox, all of this source is 100% platform independent. (The actual sample code on the Notes C API Toolkit CD ROM contains a macro that hides platform specific initialization.)
All output from this program goes to a text file, which is created and written using the standard C file i/o routines. Again, this choice of file i/o was made for platform independence. In Notes API applications, one may choose to use conditionally compiled code to add platform specific file system functionality such as MacOS file types.
NotesInitExtended() is a Notes API function that initializes the Notes runtime library. At the end of main(), NotesTerm() is called. Every standalone program must have one set of init and term calls. Multi-threaded applications must have one set of calls for each thread. In this program, NotesTerm() is called as part of the macro API_RETURN().
The database is opened using NSFDbOpen(), processed with the function ReadDatabase(), and then closed using NSFDbClose(). The open function returns a “handle” to the database, which is then used by all functions that refer to that database. NSFDbOpen() takes the name of database as a full pathname, or as a filename if that database is stored locally. If the database is stored on a Notes server, there is an API function that will build the pathname.*/
void main(int argc, char *argv[])
{
DBHANDLE db_handle; // handle of source database
STATUS error = NOERROR; // return status from API calls
#ifdef MAC
InitMacToolBox();
#endif
if ((outfile = fopen("book.src", "w")) != NULL)
{
//
// Output data that is required by the Apple Book Maker application, for the
// creation of a Newton Book.
//
// For more information, please refer to the “Newton Book Maker User’s Guide”,
// which is included with the Apple’s Newton Toolkit for MacOS and Windows.
//
fprintf(outfile,
"\
.isbn Notes:LOTUS\n\
.date 02/05/96\n\
.author Rick Gansler\n\
.publisher Lotus Development Corp.\n\
.copyright (c) 1996 Lotus Development Corp.\n\
.shorttitle API User\n\
.title Lotus Notes – API User's Guide\n\
.blurb\n\
The Lotus Notes API User's Guide exported from Notes to
Newton Book format.\n\
.layout Indented 1 Sidebar 11\n\
.layout TitlePage 12 NoTitle\n\
.layout ContentsPage 2 Sidebar 10 Main NoTitle\n\
.layout Default 12\n\
");
// Initialize the Notes runtime library
if (NotesInitExtended(argc, argv))
NOTES_INIT_ERROR;
// Open the database
if (error = NSFDbOpen("API40UG.NSF", &db_handle))
API_RETURN(ERR(error));
field_text = (void *)malloc(32000);
// Generate the Newton Book contents
uniqueLabelCounter = 0;
error = ReadDatabase(db_handle);
if (error)
{
NSFDbClose(db_handle));
API_RETURN(ERR(error));
}
// Close the database
if (error = NSFDbClose(db_handle))
API_RETURN(ERR(error));
fclose(outfile);
free(field_text);
}
API_RETURN(NOERROR);
}
/*
ReadDatabase
ReadDatabase() takes a database handle, and reads each document in the database. In this case, the database has a view that is defined in the database. One could also create a custom view at runtime. The pairing of a view and a collection is analogous to a search result that has been sorted. Once the collection has been opened, ReadEntries() is called to return a buffer containing a reference to each document in the collection, plus a counter that indicates how many documents are in the collection. Each document is identified by its unique note id. A loop is used to iterate through the buffer and pull out each note id, which is passed to the function ReadNote().*/
STATUS ReadDatabase(DBHANDLE db_handle)
{
STATUS error=NOERROR; // return status from API calls
NOTEID ViewID; // note id of the view
HCOLLECTION hCollection; // collection handle
COLLECTIONPOSITION CollPosition; // index into collection
HANDLE hBuffer; // handle to buffer of info
DWORD EntriesFound; // number of entries found
WORD SignalFlag; // signal and share warning flags
BYTE *pBuffer; // pointer into info buffer
DWORD I; // a counter
NOTEID EntryID; // a collection entry id
// Get the note id of the view we want
if (error =
NIFFindView(db_handle,"TABLE OF CONTENTS",&ViewID))
return(error);
// Get the current collection using this view
if (error = NIFOpenCollection(
db_handle, // handle of db with view
db_handle, // handle of db with data
ViewID, // note id of the view
0, // collection open flags
NULLHANDLE, // handle to unread ID list (input & return)
&hCollection, // collection handle (return)
NULLHANDLE, // handle to open view note (return)
NULL, // universal note id of view (return)
NULLHANDLE, // handle to collapsed list (return)
NULLHANDLE)) // handle to selected list (return)
return(error);
// Set a COLLECTIONPOSITION to the beginning of the collection
CollPosition.Level = 0;
CollPosition.Tumbler[0] = 0;
// Get the note ID and summary of every entry in the collection. In the
// returned buffer, first comes all of the info about the first entry, then
// all of the info about the 2nd entry, etc. For each entry, the info is
// arranged in the order of the bits in the READ_MASKs.
do
{
if (error = NIFReadEntries(
hCollection, // handle to this collection
&CollPosition, // where to start in collection
NAVIGATE_NEXT, // order to use when skipping
1L, // number to skip
NAVIGATE_NEXT, // order to use when reading
0xFFFFFFFF, // max number to read
READ_MASK_NOTEID, // info we want
&hBuffer, // handle to info buffer (return)
NULL, // length of info buffer (return)
NULL, // entries skipped (return)
&EntriesFound, // entries read (return)
&SignalFlag)) // share warning & more signal flag return
{
NIFCloseCollection(hCollection);
return(error);
}
// Check to make sure there was a buffer of information returned
if (hBuffer == NULLHANDLE)
{
NIFCloseCollection(hCollection);
NSFDbClose(db_handle);
return(NOERROR);
}
//
// Lock down (freeze the location) of the information buffer. Cast
// the resulting pointer to the type we need.
//
// OSLockObject() is sort of like the Notes equivalent of locking a
// Mac handle and then dereferencing it. However, Notes abstracts memory
// management, since each Notes platform may implement it
// differently. For example, a Windows handle is a very different kind
// of data object than a Mac handle.
//
pBuffer = (BYTE *) OSLockObject (hBuffer);
// Start a loop that extracts the info about each collection entry from
// the information buffer
for (i = 1; i <= EntriesFound; i++)
{
// Get the NoteID of this entry
EntryID = *(NOTEID*)pBuffer;
// Advance the pointer over the NoteID
pBuffer += sizeof(NOTEID);
if (! (NOTEID_CATEGORY & EntryID))
ReadNote(EntryID, db_handle);
}
// Unlock the list of NoteIDs.
OSUnlockObject(hBuffer);
// Free the memory allocated by NIFReadEntries
OSMemFree(hBuffer);
} while (SignalFlag & SIGNAL_MORE_TO_DO);
// Close the collection
error = NIFCloseCollection(hCollection);
return(error);
}
/*ReadNote
ReadNote() takes a note id and a database handle, opens the document referred to by the note id, reads only the fields in that document that are needed, formats them, and outputs them to a text file in a form that can be imported by the Apple program Book Maker.*/
STATUS far PASCAL ReadNote(NOTEID noteID, DBHANDLE db_handle)
{
NOTEHANDLE note_hdl;
WORD field_len;
long sectionNum_num;
long chapterNum_num;
char sectionName_text[100];
char titleName_text[100];
STATUS error;
static long prevSectionNumber = –1; // this is a static
// Open the document whose note id was passed to this function
if (error = NSFNoteOpen(db_handle, noteID, 0, ¬e_hdl))